package com.ruoyi.util;

import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-01-13 21:45:03
 */
public class DateUtil {
    public static final String DATE_FORMAT = "yyyyMMdd";

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd hh:mm:ss";

    public static final ZoneId DEFAULT_ZONE_ID = ZoneId.of("+8");

    public static final ZoneOffset DEFAULT_ZONE_OFFSET = ZoneOffset.of("+8");

    public static LocalDateTime timeMillisToDate(long timeMillis) {
        Instant instant = Instant.ofEpochMilli(timeMillis);
        return LocalDateTime.ofInstant(instant, DEFAULT_ZONE_OFFSET);
    }

    /**
     * LocalDateTime 对象格式化
     *
     * @param localDateTime
     * @param formatter
     * @return
     */
    public static String format(LocalDateTime localDateTime, String formatter) {
        return DateTimeFormatter.ofPattern(formatter).format(localDateTime);
    }

    /**
     * 获取LocalDateTime对应的毫秒数
     *
     * @param localDateTime
     * @return
     */
    public static long getMilliSeconds(LocalDateTime localDateTime) {
        return localDateTime.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli();
    }


    /**
     * 获取一个月最后一天的最后一秒时间
     *
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getMonthLastTemporal(LocalDateTime localDateTime) {
        return localDateTime.toLocalDate()
                .with(TemporalAdjusters.lastDayOfMonth())
                .atTime(LocalTime.MAX);
    }

    /**
     * 获取今日开始时间
     *
     * @return
     */
    public static LocalDateTime getTodayBegin() {
        return LocalDate.now().atStartOfDay();
    }

    /**
     * 根据localDateTime获取纳秒
     *
     * @param localDateTime
     * @return
     */
    public static long getNanoByLocalDateTime(LocalDateTime localDateTime) {
        return TimeUnit.MILLISECONDS.toNanos(localDateTime.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli());
    }

    /**
     * 获取bigInteger的纳秒
     *
     * @param localDateTime
     * @return
     */
    public static BigInteger getNanoBigIntegerByTime(LocalDateTime localDateTime) {
        return BigInteger.valueOf(getNanoByLocalDateTime(localDateTime));
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static LocalDateTime getCurrentTime() {
        return LocalDateTime.now(DEFAULT_ZONE_ID);
    }

    /**
     * 获取时间string类型
     *
     * @param d      时间
     * @param format 格式
     */
    public static String getDate(Date d, String format) {
        if (null == d) {
            return "";
        }
        if (null == format || "".equals(format)) {
            format = DATE_FORMAT;
        }
        DateFormat df = new SimpleDateFormat(format);
        return df.format(d);
    }

    /**
     * 获取时间string类型
     */
    public static String getNowDefault() {
        DateFormat df = new SimpleDateFormat(DATE_FORMAT);
        return df.format(new Date());
    }
}
